<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>TypeScript学习笔记 | 小康-技术博客</title><meta name="author" content="小康"><meta name="copyright" content="小康"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="TypeScript学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript学习笔记">
<meta property="og:url" content="https://codeslive.top/2023/05/26/TypeScript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="小康-技术博客">
<meta property="og:description" content="TypeScript学习笔记">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101140037.webp">
<meta property="article:published_time" content="2023-05-26T11:06:31.963Z">
<meta property="article:modified_time" content="2023-06-10T03:40:25.529Z">
<meta property="article:author" content="小康">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101140037.webp"><link rel="shortcut icon" href="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101307395.webp?x-oss-process=style/webp"><link rel="canonical" href="https://codeslive.top/2023/05/26/TypeScript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'TypeScript学习笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-06-10 11:40:25'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="小康-技术博客" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101307395.webp?x-oss-process=style/webp" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">16</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">5</div></a></div><hr/></div></div><div class="post" id="body-wrap"><header class="not-top-img fixed" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小康-技术博客"><span class="site-name">小康-技术博客</span></a></span><div id="menus"><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">TypeScript学习笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-05-26T11:06:31.963Z" title="Created 2023-05-26 19:06:31">2023-05-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-06-10T03:40:25.529Z" title="Updated 2023-06-10 11:40:25">2023-06-10</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="TypeScript学习笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h3 id="1-安装TS"><a href="#1-安装TS" class="headerlink" title="1. 安装TS"></a>1. 安装TS</h3><ol>
<li>全局安装</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install typescript@latest -g</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>在根目录下生成 <code>tsconfig.json</code> 文件</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tsc  --init</span><br></pre></td></tr></table></figure>

<p>以上这一步一定是要运行的，否则 <code>vscode</code> 会出现变量错误</p>
<p><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202304221958811.webp?x-oss-process=style/webp"></p>
<ol start="3">
<li>启动</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tsc -w 文件名</span><br></pre></td></tr></table></figure>

<h3 id="2-TS的类型声明"><a href="#2-TS的类型声明" class="headerlink" title="2. TS的类型声明"></a>2. TS的类型声明</h3><ol>
<li>可以把类型用在<code>变量</code>当中</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//变量的使用</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">num</span>: number = <span class="number">123</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">123</span>);</span><br><span class="line"><span class="keyword">let</span> <span class="attr">str</span>: string = <span class="string">&#x27;123&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str);</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>可以把类型用在<code>函数</code>中，如函数的参数、返回值</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//函数的使用</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">a: <span class="built_in">number</span>, b: <span class="built_in">number</span></span>): <span class="built_in">number</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> result = <span class="title function_">sum</span>(<span class="number">123</span>, <span class="number">456</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result);</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>TS可以用的其他类型</li>
</ol>
<p><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202304221959617.webp?x-oss-process=style/webp"></p>
<ol start="4">
<li>可以使用 <code>| </code>来连接多个类型 (联合类型)</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="attr">b</span>: <span class="string">&quot;male&quot;</span> | <span class="string">&quot;female&quot;</span>;</span><br><span class="line">b = <span class="string">&quot;male&quot;</span>;</span><br><span class="line">b = <span class="string">&quot;female&quot;</span>;</span><br><span class="line">b = <span class="string">&quot;123&quot;</span> <span class="comment">//这是不行的</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//boolean | string</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">c</span>: <span class="built_in">boolean</span> | <span class="built_in">string</span>;</span><br><span class="line">c = <span class="literal">true</span>;</span><br><span class="line">c = <span class="string">&#x27;hello&#x27;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><code>any</code>表示默认关闭TS</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//any 表示的是任意类型, 一个变量设置类型为 any 后相当于对该变量关闭TS的类型检测, 使用TS时, 不建议使用 any 类型。</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">d</span>: <span class="built_in">any</span>;</span><br><span class="line">d = <span class="number">10</span>;</span><br><span class="line">d = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">d = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果变量后面不加 any, 会自动加上。</span></span><br><span class="line"><span class="keyword">let</span> f;</span><br><span class="line">f = <span class="number">10</span>;</span><br><span class="line">f = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">f = <span class="literal">true</span>;</span><br></pre></td></tr></table></figure>

<ol start="6">
<li><code>unknown</code>表示未知类型的值</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//unknown 表示未知类型的值</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">e</span>: <span class="built_in">unknown</span>;</span><br><span class="line">e=<span class="number">10</span>;</span><br><span class="line">e=<span class="string">&quot;hello&quot;</span>;</span><br><span class="line">e=<span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//unknown 不能把定义好的类型赋值给 unknown 的变量</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">s</span>:<span class="built_in">string</span> = <span class="string">&#x27;123&#x27;</span>;</span><br><span class="line">s=e; <span class="comment">//报错</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// unknown 实际上就是一个类型安全的any</span></span><br><span class="line"><span class="comment">// unknown 类型的变量，不能直接赋值给其他变量</span></span><br><span class="line"><span class="comment">//解决报错 s = e 的办法, 赋值前进行类型检测, 这样就不会报错了</span></span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> e === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">  s = e;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//类型断言, 可以用来告诉解析器变量的实际类型, 也可以解决 s = e 报错</span></span><br><span class="line">s = e <span class="keyword">as</span> <span class="built_in">string</span>;</span><br><span class="line">s = &lt;<span class="built_in">string</span>&gt;e;</span><br></pre></td></tr></table></figure>

<ol start="7">
<li><code>void</code>用来表示空</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// void 用来表示空, 一函数为例, 就表示没有返回值的函数, 但可以返回 undefined</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>): <span class="built_in">void</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">undefined</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="8">
<li><code>never</code> 表示永远不会返回结果</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// never 表示永远不会返回结果</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn2</span>(<span class="params"></span>): <span class="built_in">never</span> &#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&quot;报错了! &quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="9">
<li><code>object</code> 表示一个 js 对象</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="attr">a</span>: <span class="built_in">object</span>;</span><br><span class="line">a = &#123;&#125;;</span><br><span class="line">a = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// &#123;&#125; 用来指定对象中可以包含哪些属性</span></span><br><span class="line"><span class="comment">// 语法：&#123;属性名:属性值, 属性名:属性值&#125;</span></span><br><span class="line"><span class="comment">// 在属性名后边加上?, 表示属性是可选的</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">g</span>: &#123; <span class="attr">name</span>: <span class="built_in">string</span>, age?: <span class="built_in">number</span> &#125;;</span><br><span class="line">g = &#123; <span class="attr">name</span>: <span class="string">&#x27;孙悟空&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// [propName: string]: any</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">h</span>: &#123; <span class="attr">name</span>: <span class="built_in">string</span>, [<span class="attr">propName</span>: <span class="built_in">string</span>]: <span class="built_in">any</span> &#125;</span><br><span class="line">h = &#123;<span class="attr">name</span>:<span class="string">&#x27;猪八戒&#x27;</span>, <span class="attr">age</span>:<span class="number">18</span>, <span class="attr">gender</span>:<span class="string">&#x27;男&#x27;</span>&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//比较常用的写法</span></span><br><span class="line"><span class="comment">// [propName: string]: any</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">h</span>: &#123; <span class="attr">name</span>: <span class="built_in">string</span>, [<span class="attr">propName</span>: <span class="built_in">string</span>]: <span class="built_in">any</span> &#125;</span><br><span class="line">h = &#123; <span class="attr">name</span>: <span class="string">&#x27;猪八戒&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>, <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置函数结构的类型声明</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">j</span>: <span class="function">(<span class="params">a: <span class="built_in">string</span>, b: <span class="built_in">string</span></span>) =&gt;</span> <span class="built_in">number</span>;</span><br><span class="line">j = <span class="keyword">function</span> (<span class="params">n1: <span class="built_in">string</span>, n2: <span class="built_in">string</span></span>): <span class="built_in">number</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// string 表示字符串数组</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">v</span>: <span class="built_in">string</span>[];</span><br><span class="line">v = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">//number[] 表示数值</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">w</span>: <span class="built_in">number</span>[];</span><br><span class="line">w = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">m</span>: <span class="title class_">Array</span>&lt;<span class="built_in">number</span>&gt;;</span><br><span class="line">m = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br></pre></td></tr></table></figure>

<ol start="10">
<li>元组</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//元组就是固定长度的数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">h</span>: [<span class="built_in">string</span>, <span class="built_in">number</span>]; <span class="comment">//可以写多个类型</span></span><br><span class="line">h = [<span class="string">&#x27;hello&#x27;</span>, <span class="number">123</span>];</span><br></pre></td></tr></table></figure>

<ol start="11">
<li><code>enum</code>枚举</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Gender</span> &#123;</span><br><span class="line">  <span class="title class_">Male</span>,</span><br><span class="line">  <span class="title class_">Female</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">i</span>: &#123; <span class="attr">name</span>: <span class="built_in">string</span>, <span class="attr">gender</span>: <span class="title class_">Gender</span> &#125;;</span><br><span class="line">i = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;孙悟空&#x27;</span>,</span><br><span class="line">  <span class="attr">gender</span>:<span class="title class_">Gender</span>.<span class="property">Male</span> </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(i.<span class="property">gender</span> === <span class="title class_">Gender</span>.<span class="property">Male</span>); <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<h3 id="3-TS的编译选项（tsconfig-json）"><a href="#3-TS的编译选项（tsconfig-json）" class="headerlink" title="3. TS的编译选项（tsconfig.json）"></a>3. TS的编译选项（tsconfig.json）</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="comment">/* </span></span><br><span class="line"><span class="comment">      tsconfig.json 是ts编译器的配置文件，ts编译器可以更具它的信息来对代码进行编译</span></span><br><span class="line"><span class="comment">      &quot;include&quot; 用来表示指定哪些ts文件需要被编译</span></span><br><span class="line"><span class="comment">      路径；</span></span><br><span class="line"><span class="comment">          *表示任意文件，</span></span><br><span class="line"><span class="comment">          **表示任意目录</span></span><br><span class="line"><span class="comment">      &quot;exclude&quot; 用来表示不需要被编译的文件目录</span></span><br><span class="line"><span class="comment">              默认值：&#123;&quot;node_modules&quot;, &quot;bower_components&quot;, &quot;jspm_packages&quot;&#125;</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">      <span class="string">&quot;include&quot;</span>: [</span><br><span class="line">          <span class="string">&quot;./src/**/*&quot;</span></span><br><span class="line">      ],</span><br><span class="line">      </span><br><span class="line">      <span class="comment">// &quot;exclude&quot;: [</span></span><br><span class="line">      <span class="comment">//     &quot;./src/hello/**/*&quot;</span></span><br><span class="line">      <span class="comment">// ]</span></span><br><span class="line">  </span><br><span class="line">      <span class="comment">//compilerOptions 编译器的选项 </span></span><br><span class="line">      <span class="string">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">          <span class="comment">//target 用来指定ts被编译为ES的版本</span></span><br><span class="line">          <span class="comment">//&#x27;es3&#x27;, &#x27;es5&#x27;, &#x27;es6&#x27;, &#x27;es2015&#x27;, &#x27;es2016&#x27;, &#x27;es2017&#x27;, &#x27;es2018&#x27;, &#x27;es2019&#x27;, &#x27;es2020&#x27;, &#x27;esnext&#x27;.</span></span><br><span class="line">          <span class="string">&quot;target&quot;</span>: <span class="string">&quot;ES2015&quot;</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">//module 指定要使用的模块化的规范</span></span><br><span class="line">          <span class="comment">//&#x27;none&#x27;, &#x27;commonjs&#x27;, &#x27;amd&#x27;, &#x27;system&#x27;, &#x27;umd&#x27;, &#x27;es6&#x27;, &#x27;es2015&#x27;, &#x27;es2020&#x27;, &#x27;esnext&#x27;</span></span><br><span class="line">          <span class="string">&quot;module&quot;</span>: <span class="string">&quot;es2015&quot;</span>,</span><br><span class="line">          </span><br><span class="line">          <span class="comment">//lib 用来指定项目所用的库</span></span><br><span class="line">          <span class="comment">// &#x27;es5&#x27;, &#x27;es6&#x27;, &#x27;es2015&#x27;, &#x27;es7&#x27;, &#x27;es2016&#x27;, &#x27;es2017&#x27;, &#x27;es2018&#x27;, &#x27;es2019&#x27;, &#x27;es2020&#x27;, &#x27;esnext&#x27;, &#x27;dom&#x27;, </span></span><br><span class="line">          <span class="comment">// &#x27;dom.iterable&#x27;, &#x27;webworker&#x27;, &#x27;webworker.importscripts&#x27;, &#x27;webworker.iterable&#x27;, &#x27;scripthost&#x27;, &#x27;es2015.core&#x27;, &#x27;es2015.collection&#x27;, </span></span><br><span class="line">          <span class="comment">// &#x27;es2015.generator&#x27;, &#x27;es2015.iterable&#x27;, &#x27;es2015.promise&#x27;, &#x27;es2015.proxy&#x27;, &#x27;es2015.reflect&#x27;, &#x27;es2015.symbol&#x27;, &#x27;es2015.symbol.wellknown&#x27;, &#x27;es2016.array.include&#x27;,</span></span><br><span class="line">          <span class="comment">//  &#x27;es2017.object&#x27;, &#x27;es2017.sharedmemory&#x27;, &#x27;es2017.string&#x27;, &#x27;es2017.intl&#x27;, &#x27;es2017.typedarrays&#x27;, &#x27;es2018.asyncgenerator&#x27;, &#x27;es2018.asynciterable&#x27;, &#x27;es2018.intl&#x27;, </span></span><br><span class="line">          <span class="comment">// &#x27;es2018.promise&#x27;, &#x27;es2018.regexp&#x27;, &#x27;es2019.array&#x27;, &#x27;es2019.object&#x27;, &#x27;es2019.string&#x27;, &#x27;es2019.symbol&#x27;, &#x27;es2020.bigint&#x27;, &#x27;es2020.promise&#x27;, </span></span><br><span class="line">          <span class="comment">// &#x27;es2020.sharedmemory&#x27;, &#x27;es2020.string&#x27;, &#x27;es2020.symbol.wellknown&#x27;, &#x27;es2020.intl&#x27;, &#x27;esnext.array&#x27;, &#x27;esnext.symbol&#x27;, &#x27;esnext.asynciterable&#x27;, &#x27;esnext.intl&#x27;,</span></span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">          <span class="comment">//outDir 用来指定编译后文件所在的目录</span></span><br><span class="line">          <span class="string">&quot;outDir&quot;</span>: <span class="string">&quot;./dist&quot;</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">//outFile 将代码合并为一个文件</span></span><br><span class="line">          <span class="comment">// &quot;outFile&quot;: &quot;./dist/app.js&quot;,</span></span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 是否对js文件进行编译，默认是false</span></span><br><span class="line">          <span class="string">&quot;allowJs&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 是否检查js代码是否符合语法规范，默认值是false</span></span><br><span class="line">          <span class="string">&quot;checkJs&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 是否移除注释</span></span><br><span class="line">          <span class="string">&quot;removeComments&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 不生成编译后的文件</span></span><br><span class="line">          <span class="string">&quot;noEmit&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 当有错误时不生成编译文件</span></span><br><span class="line">          <span class="string">&quot;noEmitOnError&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 所有严格检查的总开关</span></span><br><span class="line">          <span class="string">&quot;strict&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 用来设置编译后的文件是否使用严格模式，默认是false</span></span><br><span class="line">          <span class="string">&quot;alwaysStrict&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 不允许隐式的any类型</span></span><br><span class="line">          <span class="string">&quot;noImplicitAny&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 不允许不明确类型的this</span></span><br><span class="line">          <span class="string">&quot;noImplicitThis&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  </span><br><span class="line">          <span class="comment">// 严格检查空值</span></span><br><span class="line">          <span class="string">&quot;strictNullChecks&quot;</span>: <span class="literal">true</span></span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-webpack打包TS"><a href="#4-webpack打包TS" class="headerlink" title="4. webpack打包TS"></a>4. webpack打包TS</h3><ol>
<li>安装webpack</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i -D webpack-cli typescript ts-loader</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>自动生成html</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i -D html-webpack-plugin</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>自动响应浏览器更新</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i -D webpack-dev-server</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>清除dist目录旧文件</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i -D clean-webpack-plugin</span><br></pre></td></tr></table></figure>

<ol start="5">
<li>webpack 中所有的配置信息都应该写在<code>module.exports</code>中</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = &#123;</span><br><span class="line">    <span class="attr">mode</span>: <span class="string">&#x27;none&#x27;</span>,</span><br><span class="line">    <span class="comment">// 指定入口文件</span></span><br><span class="line">    <span class="attr">entry</span>: <span class="string">&quot;./src/index.ts&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 指定打包文件所在目录</span></span><br><span class="line">    <span class="attr">output</span>: &#123;</span><br><span class="line">        <span class="comment">//指定打包文件的目录</span></span><br><span class="line">        <span class="attr">path</span>: path.<span class="title function_">resolve</span>(__dirname, <span class="string">&#x27;dist&#x27;</span>),</span><br><span class="line">        <span class="comment">//打包后文件的名字</span></span><br><span class="line">        <span class="attr">filename</span>: <span class="string">&quot;bundle.js&quot;</span>,</span><br><span class="line">        <span class="comment">//告诉webpack不使用箭头函数</span></span><br><span class="line">        <span class="attr">environment</span>: &#123;</span><br><span class="line">            <span class="attr">arrowFunction</span>: <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//指定webpack打包时要使用的模块</span></span><br><span class="line">    <span class="attr">module</span>: &#123;</span><br><span class="line">        <span class="comment">// 指定要loader加载的规则</span></span><br><span class="line">        <span class="attr">rules</span>: [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// test指定的时规则生效的文件</span></span><br><span class="line">                <span class="attr">test</span>: <span class="regexp">/\.ts$/</span>,<span class="comment">//以ts结尾的文件</span></span><br><span class="line">                <span class="comment">// 要使用的loader</span></span><br><span class="line">                <span class="attr">use</span>: [</span><br><span class="line">                    <span class="comment">// 配置babel</span></span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="comment">//指定加载器</span></span><br><span class="line">                        <span class="attr">loader</span>: <span class="string">&quot;babel-loader&quot;</span>,</span><br><span class="line">                        <span class="comment">// 设置babel</span></span><br><span class="line">                        <span class="attr">options</span>: &#123;</span><br><span class="line">                            <span class="comment">//设置预定义的环境</span></span><br><span class="line">                            <span class="attr">presets</span>: [</span><br><span class="line">                                [</span><br><span class="line">                                    <span class="comment">//指定环境的插件</span></span><br><span class="line">                                    <span class="string">&quot;@babel/preset-env&quot;</span>,</span><br><span class="line">                                    <span class="comment">// 配置信息</span></span><br><span class="line">                                    &#123;</span><br><span class="line">                                        <span class="comment">// 要兼容的目标浏览器及版本</span></span><br><span class="line">                                        <span class="attr">targets</span>: &#123;</span><br><span class="line">                                            <span class="string">&quot;chrome&quot;</span>: <span class="string">&quot;58&quot;</span>,</span><br><span class="line">                                            <span class="string">&quot;ie&quot;</span>: <span class="string">&quot;11&quot;</span></span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        <span class="comment">//指定corejs的版本</span></span><br><span class="line">                                        <span class="string">&quot;corejs&quot;</span>: <span class="string">&quot;3&quot;</span>,</span><br><span class="line">                                        <span class="comment">//使用corejs的方式 &quot;usage&quot;  表示按需加载</span></span><br><span class="line">                                        <span class="string">&quot;useBuiltIns&quot;</span>: <span class="string">&quot;usage&quot;</span> </span><br><span class="line">                                    &#125;</span><br><span class="line"></span><br><span class="line">                                ]</span><br><span class="line">                            ]</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="comment">// &#x27;babel-loader&#x27;,</span></span><br><span class="line">                    <span class="string">&#x27;ts-loader&#x27;</span></span><br><span class="line">                ],</span><br><span class="line">                <span class="comment">// 要排除的文件</span></span><br><span class="line">                <span class="attr">exclude</span>: <span class="regexp">/node-modules/</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="comment">//配置Webpack 插件</span></span><br><span class="line">    <span class="attr">plugins</span>: [</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">CleanWebpackPlugin</span>(),</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">HTMLWebpackPlugin</span>(&#123;</span><br><span class="line">            <span class="comment">// title: &quot;这是一个自定义的title&quot;、</span></span><br><span class="line">            <span class="attr">template</span>: <span class="string">&quot;./src/index.html&quot;</span> </span><br><span class="line">        &#125;),</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 用来设置引用模块，可以将这些文件识别为模块</span></span><br><span class="line">    <span class="attr">resolve</span>: &#123;</span><br><span class="line">        <span class="attr">extensions</span>: [<span class="string">&#x27;.ts&#x27;</span>, <span class="string">&#x27;.js&#x27;</span>]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-面向对象"><a href="#5-面向对象" class="headerlink" title="5. 面向对象"></a>5. 面向对象</h3><p>面向对象是程序中一个非常重要的思想，它被很多同学理解成了一个比较难，比较深奥的问题，其实不然。面向对象很简单，简而言之就是程序之中所有的操作都需要通过对象来完成。</p>
<ul>
<li>举例来说：<ul>
<li>操作浏览器要使用window对象</li>
<li>操作网页要使用document对象</li>
<li>操作控制台要使用console对象</li>
</ul>
</li>
</ul>
<p>一切操作都要通过对象，也就是所谓的面向对象，那么对象到底是什么呢？这就要先说到程序是什么，计算机程序的本质就是对现实事物的抽象，抽象的反义词是具体，比如：照片是对一个具体的人的抽象，汽车模型是对具体汽车的抽象等等。程序也是对事物的抽象，在程序中我们可以表示一个人、一条狗、一把枪、一颗子弹等等所有的事物。一个事物到了程序中就变成了一个对象。</p>
<p>在程序中所有的对象都被分成了两个部分数据和功能，以人为例，人的姓名、性别、年龄、身高、体重等属于数据，人可以说话、走路、吃饭、睡觉这些属于人的功能。数据在对象中被成为属性，而功能就被称为方法。所以简而言之，在程序中一切皆是对象。</p>
<h5 id="1、类（class）"><a href="#1、类（class）" class="headerlink" title="1、类（class）"></a>1、类（class）</h5><p>要想面向对象，操作对象，首先便要拥有对象，那么下一个问题就是如何创建对象。要创建对象，必须要先定义类，所谓的类可以理解为对象的模型，程序中可以根据类创建指定类型的对象，举例来说：可以通过Person类来创建人的对象，通过Dog类创建狗的对象，通过Car类来创建汽车的对象，不同的类可以用来创建不同的对象。</p>
<ul>
<li><p>定义类：</p>
<ul>
<li><pre><code class="typescript">class 类名 &#123;
    属性名: 类型;
    
    constructor(参数: 类型)&#123;
        this.属性名 = 参数;
    &#125;
    
    方法名()&#123;
        ....
    &#125;

&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 示例：</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    class Person&#123;</span><br><span class="line">        name: string;</span><br><span class="line">        age: number;</span><br><span class="line">    </span><br><span class="line">        constructor(name: string, age: number)&#123;</span><br><span class="line">            this.name = name;</span><br><span class="line">            this.age = age;</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        sayHello()&#123;</span><br><span class="line">            console.log(`大家好，我是$&#123;this.name&#125;`);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
</ul>
</li>
<li><p>使用类：</p>
<ul>
<li><pre><code class="typescript">const p = new Person(&#39;孙悟空&#39;, 18);
p.sayHello();
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">##### 2、面向对象的特点</span><br><span class="line"></span><br><span class="line">- 封装</span><br><span class="line"></span><br><span class="line">  - 对象实质上就是属性和方法的容器，它的主要作用就是存储属性和方法，这就是所谓的封装</span><br><span class="line"></span><br><span class="line">  - 默认情况下，对象的属性是可以任意的修改的，为了确保数据的安全性，在TS中可以对属性的权限进行设置</span><br><span class="line"></span><br><span class="line">  - 只读属性（readonly）：</span><br><span class="line"></span><br><span class="line">    - 如果在声明属性时添加一个readonly，则属性便成了只读属性无法修改</span><br><span class="line"></span><br><span class="line">  - TS中属性具有三种修饰符：</span><br><span class="line"></span><br><span class="line">    - public（默认值），可以在类、子类和对象中修改</span><br><span class="line">    - protected ，可以在类、子类中修改</span><br><span class="line">    - private ，可以在类中修改</span><br><span class="line"></span><br><span class="line">  - 示例：</span><br><span class="line"></span><br><span class="line">    - public</span><br><span class="line"></span><br><span class="line">      - ```typescript</span><br><span class="line">        class Person&#123;</span><br><span class="line">            public name: string; // 写或什么都不写都是public</span><br><span class="line">            public age: number;</span><br><span class="line">        </span><br><span class="line">            constructor(name: string, age: number)&#123;</span><br><span class="line">                this.name = name; // 可以在类中修改</span><br><span class="line">                this.age = age;</span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">            sayHello()&#123;</span><br><span class="line">                console.log(`大家好，我是$&#123;this.name&#125;`);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        class Employee extends Person&#123;</span><br><span class="line">            constructor(name: string, age: number)&#123;</span><br><span class="line">                super(name, age);</span><br><span class="line">                this.name = name; //子类中可以修改</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        const p = new Person(&#x27;孙悟空&#x27;, 18);</span><br><span class="line">        p.name = &#x27;猪八戒&#x27;;// 可以通过对象修改</span><br></pre></td></tr></table></figure>

- protected

  - ```typescript
    class Person&#123;
        protected name: string;
        protected age: number;
    
        constructor(name: string, age: number)&#123;
            this.name = name; // 可以修改
            this.age = age;
        &#125;
    
        sayHello()&#123;
            console.log(`大家好，我是$&#123;this.name&#125;`);
        &#125;
    &#125;
    
    class Employee extends Person&#123;
    
        constructor(name: string, age: number)&#123;
            super(name, age);
            this.name = name; //子类中可以修改
        &#125;
    &#125;
    
    const p = new Person(&#39;孙悟空&#39;, 18);
    p.name = &#39;猪八戒&#39;;// 不能修改
    <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- private</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    class Person&#123;</span><br><span class="line">        private name: string;</span><br><span class="line">        private age: number;</span><br><span class="line">    </span><br><span class="line">        constructor(name: string, age: number)&#123;</span><br><span class="line">            this.name = name; // 可以修改</span><br><span class="line">            this.age = age;</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        sayHello()&#123;</span><br><span class="line">            console.log(`大家好，我是$&#123;this.name&#125;`);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    class Employee extends Person&#123;</span><br><span class="line">    </span><br><span class="line">        constructor(name: string, age: number)&#123;</span><br><span class="line">            super(name, age);</span><br><span class="line">            this.name = name; //子类中不能修改</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    const p = new Person(&#x27;孙悟空&#x27;, 18);</span><br><span class="line">    p.name = &#x27;猪八戒&#x27;;// 不能修改</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
<li><p>属性存取器</p>
<ul>
<li><p>对于一些不希望被任意修改的属性，可以将其设置为private</p>
</li>
<li><p>直接将其设置为private将导致无法再通过对象修改其中的属性</p>
</li>
<li><p>我们可以在类中定义一组读取、设置属性的方法，这种对属性读取或设置的属性被称为属性的存取器</p>
</li>
<li><p>读取属性的方法叫做setter方法，设置属性的方法叫做getter方法</p>
</li>
<li><p>示例：</p>
<ul>
<li><pre><code class="typescript">class Person&#123;
    private _name: string;

    constructor(name: string)&#123;
        this._name = name;
    &#125;

    get name()&#123;
        return this._name;
    &#125;

    set name(name: string)&#123;
        this._name = name;
    &#125;

&#125;

const p1 = new Person(&#39;孙悟空&#39;);
console.log(p1.name); // 通过getter读取name属性
p1.name = &#39;猪八戒&#39;; // 通过setter修改name属性
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 静态属性</span><br><span class="line"></span><br><span class="line">  - 静态属性（方法），也称为类属性。使用静态属性无需创建实例，通过类即可直接使用</span><br><span class="line"></span><br><span class="line">  - 静态属性（方法）使用static开头</span><br><span class="line"></span><br><span class="line">  - 示例：</span><br><span class="line"></span><br><span class="line">    - ```typescript</span><br><span class="line">      class Tools&#123;</span><br><span class="line">          static PI = 3.1415926;</span><br><span class="line">          </span><br><span class="line">          static sum(num1: number, num2: number)&#123;</span><br><span class="line">              return num1 + num2</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      console.log(Tools.PI);</span><br><span class="line">      console.log(Tools.sum(123, 456));</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
</ul>
</li>
</ul>
</li>
<li><p>this</p>
<ul>
<li>在类中，使用this表示当前对象</li>
</ul>
</li>
</ul>
</li>
<li><p>继承</p>
<ul>
<li><p>继承时面向对象中的又一个特性</p>
</li>
<li><p>通过继承可以将其他类中的属性和方法引入到当前类中</p>
<ul>
<li><p>示例：</p>
<ul>
<li><pre><code class="typescript">class Animal&#123;
    name: string;
    age: number;

    constructor(name: string, age: number)&#123;
        this.name = name;
        this.age = age;
    &#125;
&#125;

class Dog extends Animal&#123;

    bark()&#123;
        console.log(`$&#123;this.name&#125;在汪汪叫！`);
    &#125;
&#125;

const dog = new Dog(&#39;旺财&#39;, 4);
dog.bark();
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 通过继承可以在不修改类的情况下完成对类的扩展</span><br><span class="line"></span><br><span class="line">- 重写</span><br><span class="line"></span><br><span class="line">  - 发生继承时，如果子类中的方法会替换掉父类中的同名方法，这就称为方法的重写</span><br><span class="line"></span><br><span class="line">  - 示例：</span><br><span class="line"></span><br><span class="line">    - ```typescript</span><br><span class="line">      class Animal&#123;</span><br><span class="line">          name: string;</span><br><span class="line">          age: number;</span><br><span class="line">      </span><br><span class="line">          constructor(name: string, age: number)&#123;</span><br><span class="line">              this.name = name;</span><br><span class="line">              this.age = age;</span><br><span class="line">          &#125;</span><br><span class="line">      </span><br><span class="line">          run()&#123;</span><br><span class="line">              console.log(`父类中的run方法！`);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      class Dog extends Animal&#123;</span><br><span class="line">      </span><br><span class="line">          bark()&#123;</span><br><span class="line">              console.log(`$&#123;this.name&#125;在汪汪叫！`);</span><br><span class="line">          &#125;</span><br><span class="line">      </span><br><span class="line">          run()&#123;</span><br><span class="line">              console.log(`子类中的run方法，会重写父类中的run方法！`);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      const dog = new Dog(&#x27;旺财&#x27;, 4);</span><br><span class="line">      dog.bark();</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
<li><p>在子类中可以使用super来完成对父类的引用</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>抽象类（abstract class）</p>
<ul>
<li><p>抽象类是专门用来被其他类所继承的类，它只能被其他类所继承不能用来创建实例</p>
</li>
<li><pre><code class="typescript">abstract class Animal&#123;
    abstract run(): void;
    bark()&#123;
        console.log(&#39;动物在叫~&#39;);
    &#125;
&#125;

class Dog extends Animals&#123;
    run()&#123;
        console.log(&#39;狗在跑~&#39;);
    &#125;
&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    - 使用abstract开头的方法叫做抽象方法，抽象方法没有方法体只能定义在抽象类中，继承抽象类时抽象方法必须要实现</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">##### 3、接口（Interface）</span><br><span class="line"></span><br><span class="line">接口的作用类似于抽象类，不同点在于接口中的所有方法和属性都是没有实值的，换句话说接口中的所有方法都是抽象方法。接口主要负责定义一个类的结构，接口可以去限制一个对象的接口，对象只有包含接口中定义的所有属性和方法时才能匹配接口。同时，可以让一个类去实现接口，实现接口时类中要保护接口中的所有属性。</span><br><span class="line"></span><br><span class="line">- 示例（检查对象类型）：</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    interface Person&#123;</span><br><span class="line">        name: string;</span><br><span class="line">        sayHello():void;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    function fn(per: Person)&#123;</span><br><span class="line">        per.sayHello();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    fn(&#123;name:&#x27;孙悟空&#x27;, sayHello() &#123;console.log(`Hello, 我是 $&#123;this.name&#125;`)&#125;&#125;);</span><br><span class="line">    </span><br></pre></td></tr></table></figure>
</code></pre>
</li>
</ul>
</li>
</ul>
</li>
<li><p>示例（实现）</p>
<ul>
<li><pre><code class="typescript">interface Person&#123;
    name: string;
    sayHello():void;
&#125;

class Student implements Person&#123;
    constructor(public name: string) &#123;
    &#125;

    sayHello() &#123;
        console.log(&#39;大家好，我是&#39;+this.name);
    &#125;
&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  - </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">##### 4、泛型（Generic）</span><br><span class="line"></span><br><span class="line">定义一个函数或类时，有些情况下无法确定其中要使用的具体类型（返回值、参数、属性的类型不能确定），此时泛型便能够发挥作用。</span><br><span class="line"></span><br><span class="line">- 举个例子：</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    function test(arg: any): any&#123;</span><br><span class="line">    	return arg;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
<li><p>上例中，test函数有一个参数类型不确定，但是能确定的时其返回值的类型和参数的类型是相同的，由于类型不确定所以参数和返回值均使用了any，但是很明显这样做是不合适的，首先使用any会关闭TS的类型检查，其次这样设置也不能体现出参数和返回值是相同的类型</p>
</li>
<li><p>使用泛型：</p>
</li>
<li><pre><code class="typescript">function test&lt;T&gt;(arg: T): T&#123;
    return arg;
&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 这里的```&lt;T&gt;```就是泛型，T是我们给这个类型起的名字（不一定非叫T），设置泛型后即可在函数中使用T来表示该类型。所以泛型其实很好理解，就表示某个类型。</span><br><span class="line"></span><br><span class="line">- 那么如何使用上边的函数呢？</span><br><span class="line"></span><br><span class="line">  - 方式一（直接使用）：</span><br><span class="line"></span><br><span class="line">    - ```typescript</span><br><span class="line">      test(10)</span><br></pre></td></tr></table></figure>

  - 使用时可以直接传递参数使用，类型会由TS自动推断出来，但有时编译器无法自动推断时还需要使用下面的方式

- 方式二（指定类型）：

  - ```typescript
    test&lt;number&gt;(10)
    <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    - 也可以在函数后手动指定泛型</span><br><span class="line"></span><br><span class="line">- 可以同时指定多个泛型，泛型间使用逗号隔开：</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    function test&lt;T, K&gt;(a: T, b: K): K&#123;</span><br><span class="line">        return b;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    test&lt;number, string&gt;(10, &quot;hello&quot;);</span><br></pre></td></tr></table></figure>

- 使用泛型时，完全可以将泛型当成是一个普通的类去使用
</code></pre>
</li>
<li><p>类中同样可以使用泛型：</p>
<ul>
<li><pre><code class="typescript">class MyClass&lt;T&gt;&#123;
    prop: T;

    constructor(prop: T)&#123;
        this.prop = prop;
    &#125;
&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 除此之外，也可以对泛型的范围进行约束</span><br><span class="line"></span><br><span class="line">  - ```typescript</span><br><span class="line">    interface MyInter&#123;</span><br><span class="line">        length: number;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    function test&lt;T extends MyInter&gt;(arg: T): number&#123;</span><br><span class="line">        return arg.length;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
<li><p>使用T extends MyInter表示泛型T必须是MyInter的子类，不一定非要使用接口类和抽象类同样适用。</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://codeslive.top">小康</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://codeslive.top/2023/05/26/TypeScript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">https://codeslive.top/2023/05/26/TypeScript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101140037.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/05/26/NVM%EF%BC%88%E5%A4%9A%E4%B8%AAnode%E7%89%88%E6%9C%AC%E8%87%AA%E7%94%B1%E5%88%87%E6%8D%A2%EF%BC%89/" title="NVM（多个node版本自由切换）"><img class="cover" src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202305270040290.webp?x-oss-process=style/webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">NVM（多个node版本自由切换）</div></div></a></div><div class="next-post pull-right"><a href="/2023/05/26/ES6-ES11%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="ES6-ES11学习笔记"><img class="cover" src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101141945.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">ES6-ES11学习笔记</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306101307395.webp?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">小康</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">16</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/codeslive"><i class="fab fa-github"></i><span>关注我</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">别问我为什么沉默，时间比我能告诉你的更多。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%AE%89%E8%A3%85TS"><span class="toc-number">1.</span> <span class="toc-text">1. 安装TS</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-TS%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E"><span class="toc-number">2.</span> <span class="toc-text">2. TS的类型声明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-TS%E7%9A%84%E7%BC%96%E8%AF%91%E9%80%89%E9%A1%B9%EF%BC%88tsconfig-json%EF%BC%89"><span class="toc-number">3.</span> <span class="toc-text">3. TS的编译选项（tsconfig.json）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-webpack%E6%89%93%E5%8C%85TS"><span class="toc-number">4.</span> <span class="toc-text">4. webpack打包TS</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.</span> <span class="toc-text">5. 面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1%E3%80%81%E7%B1%BB%EF%BC%88class%EF%BC%89"><span class="toc-number">5.0.1.</span> <span class="toc-text">1、类（class）</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/26/JWT%EF%BC%88%E9%89%B4%E6%9D%83%EF%BC%89/" title="JWT（鉴权）"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306261848915.webp?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JWT（鉴权）"/></a><div class="content"><a class="title" href="/2023/06/26/JWT%EF%BC%88%E9%89%B4%E6%9D%83%EF%BC%89/" title="JWT（鉴权）">JWT（鉴权）</a><time datetime="2023-06-26T10:46:25.846Z" title="Created 2023-06-26 18:46:25">2023-06-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/24/SSL%20TSL%20HTTPS/" title="SSL TSL HTTPS"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306111114693.png?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SSL TSL HTTPS"/></a><div class="content"><a class="title" href="/2023/06/24/SSL%20TSL%20HTTPS/" title="SSL TSL HTTPS">SSL TSL HTTPS</a><time datetime="2023-06-24T09:55:09.942Z" title="Created 2023-06-24 17:55:09">2023-06-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/11/XSS%E8%B7%A8%E7%AB%99%E8%84%9A%E6%9C%AC%E6%94%BB%E5%87%BB/" title="XSS跨站脚本攻击"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306111114693.png?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="XSS跨站脚本攻击"/></a><div class="content"><a class="title" href="/2023/06/11/XSS%E8%B7%A8%E7%AB%99%E8%84%9A%E6%9C%AC%E6%94%BB%E5%87%BB/" title="XSS跨站脚本攻击">XSS跨站脚本攻击</a><time datetime="2023-06-11T03:10:26.561Z" title="Created 2023-06-11 11:10:26">2023-06-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/05/%E5%85%B3%E4%BA%8E%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E5%88%B7%E6%96%B0%E4%B8%A2%E5%A4%B1%E7%9A%84%E9%97%AE%E9%A2%98/" title="关于动态路由刷新丢失的问题"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306052004251.webp?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="关于动态路由刷新丢失的问题"/></a><div class="content"><a class="title" href="/2023/06/05/%E5%85%B3%E4%BA%8E%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E5%88%B7%E6%96%B0%E4%B8%A2%E5%A4%B1%E7%9A%84%E9%97%AE%E9%A2%98/" title="关于动态路由刷新丢失的问题">关于动态路由刷新丢失的问题</a><time datetime="2023-06-05T12:00:12.830Z" title="Created 2023-06-05 20:00:12">2023-06-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/05/TCP%E5%AE%9E%E7%8E%B0http%E6%9C%8D%E5%8A%A1/" title="TCP实现http服务"><img src="https://static-youdao-note.oss-cn-shenzhen.aliyuncs.com/images/202306051707718.webp?x-oss-process=style/webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="TCP实现http服务"/></a><div class="content"><a class="title" href="/2023/06/05/TCP%E5%AE%9E%E7%8E%B0http%E6%9C%8D%E5%8A%A1/" title="TCP实现http服务">TCP实现http服务</a><time datetime="2023-06-05T09:01:56.797Z" title="Created 2023-06-05 17:01:56">2023-06-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 By 小康</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a target="_blank" rel="noopener" href="//beian.miit.gov.cn/"><center>粤ICP备2021164535号-2</center></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>